package leetcode.weekly.week313;

import java.util.ArrayList;
import java.util.List;

//Solution3Test
public class Solution3 {
	public int minimizeXor(int num1, int num2) {
		System.out.println(Integer.toBinaryString(num1));
		System.out.println(Integer.toBinaryString(num2));
		int cnt = Integer.bitCount(num2);
		int x = 0;
		List<Integer> list = new ArrayList<>();
		while (num1 > 0) {
			int lb = this.lowbit(num1);
			list.add(lb);
			num1 -= lb;
		}
		int i = 0;
		int size = list.size();
		for (; i < cnt && size - 1 - i >= 0; i++) {
			x += list.get(size - 1 - i);
		}
		for (int p = 1; i < cnt;) {
			if (!list.contains(p)) {
				x += p;
				i++;
			}
			p <<= 1;
		}
		return x;
	}

	private int lowbit(int x) {
		return x & -x;
	}

}
